<?php
namespace Admin\Controller;
use Common\Controller\BaseController;
use Common\Model\CheckModel;

class CheckController extends BaseController
{
    public $action = 0;
    public $page_size = 10;

    public function _initialize()
    {
        parent::_initialize();
        self::$_action_block = 5;
        $this->assign('block',self::$_action_block);
    }

    public function index()  //对应view中的index页面
    {
        $this->action = 4;    //index页面中的action值为2
        $type = I('get.t',0,'intval');
        $where = ' 1 ';
        if(!empty($type)){
            $where .= " and status={$type} ";
            $this->action = $type;
        }
        $checkModel = D('Check');
        $count = $checkModel
                ->join('room on room.room_id=check.room_id','LEFT')
                ->where($where)
                ->count();        //获取记录数

        vendor('Page');
        $Page  = new \Vendor\Page($count,$this->page_size);
        
        $field = "check.id,check.tab_check_id,check.check_content,check.check_time,check.status,check.remark,check.read,room.room_id,room.room_name ";
        $list = $checkModel->field($field)
                ->join('room on check.room_id=room.room_id','LEFT')
                ->limit($Page->firstRow.','.$Page->listRows)
                ->where($where)
                ->order('check.id DESC')
                ->select();
        $this->assign('list',$list);
        
        $this->assign('page',$Page->show());
        $this->assign('type',$this->action);
        $this->display('index');   //显示index页面
    }
    

    public function add()    //对应view中的add 3个页面
    {
        $this->action = 4;
        $roomModel = D('Room');
        $fields = "room_id,room_name";

        $roominfo  = $roomModel->getRoom($fields);

        $this->assign('roominfo',$roominfo);
        $this->assign('now',date('Y-m-d H:i:s',time()));
        $this->assign('type',$this->action);
        $this->display('add');
        
    }

    public function save()    //save操作
    {
        $post = I('post.');     //通过post方法获取到用户填写的所有信息
        $checkModel = D('Check');     //实例化自定义的模型类
        $res =  $checkModel->addCheck($post);     //res存的是操作数据库的是否成功信息
        if(false !== $res['status'])     // !==不恒等于false，即操作成功
        {
            $this->success($res['info'],U('Admin/Check/index'));   //调用控制器的index方法
        }
        else
        {
            $this->error($res['info']);   //操作失败显示错误
        }
        exit;
    }

    public function drift()   //草稿箱操作
    {
        $this->action = 3;
        $this->assign('type',$this->action);
        $this->display('drift');
    }

    public function edit()
    {
      $this->display('edit');
    }

    public function delete()
    {
      $this->display('delete');
    }


    public function view()   //index.html 中的链接引发的view方法
    {
        $id     = I('get.id',0,'intval');   //在index.html的链接中传过来id和type值，id和type在index方法中的list变量中存在
        $type   = I('get.t',0,'intval');
        
        $checkModel  = D('Check');

        $where = "check.id={$id}";
        $field = "check.id,check.tab_check_id,check.check_content,check.check_time,check.status,check.remark,check.read,room.room_id,room.room_name ";
        $info = $checkModel->field($field)
            ->join('room on check.room_id=room.room_id','LEFT')
            ->limit($Page->firstRow.','.$Page->listRows)
            ->where($where)
            ->order('check.id DESC')
            ->limit(1)
            ->select();
        if(empty($info))
        {
            $this->error('数据不存在');
        }
        $this->assign('info',$info[0]);
        $view   = 'view_'.$type; 
        $this->display($view);    //显示某一个view页面
    }
    public function judge(){
        $status = I('post.status',3,'intval');
        $id = I('post.id',0,'intval');
        $remark = I('post.remark','','htmlspecialchars');

        $checkModel = new CheckModel();
        $res = $checkModel->judge($id,$status,$remark);
        if(false !== $res){
            $this->success('Success',U('Admin/Check/index',array('t'=>$status)));
        }else{
            $this->error('Error');
        }
    }
 
}